package net.crazyadam.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.TextView;
import net.crazyadam.R;
import net.crazyadam.audio.Constant;
import net.crazyadam.audio.DopplerAnalyser;
import net.crazyadam.network.PacketManager;

/**
 * Copyright by <a href="http://crazyadam.net"><em><i>Joseph J.C. Tang</i></em></a> <br/>
 * Email: <a href="mailto:jinntrance@gmail.com">jinntrance@gmail.com</a>
 *
 * @author joseph
 * @version 1.0
 * @since 9/26/12 5:03 PM
 */
public class FindUActivity extends Activity {
    private DopplerAnalyser dopplerAnalyser;
    private PacketManager pm;
    private boolean listening = true;
    private long mainThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dopplerAnalyser = new DopplerAnalyser(this);
        setContentView(R.layout.audio);
        pm = new PacketManager(this);
        pm.toReceive();//open the receiving service
//        pm.start();//open the receiving service
        TextView text = (TextView) findViewById(R.id.text);
        text.setText("等待发送命令");
        mainThread = Thread.currentThread().getId();
        new Thread() {
            @Override
            public void run() {
                while (listening)
                    try {
                        while (!Constant.START.equals(pm.getMsg()))
                            Thread.sleep(100);
                        //測試甩動前頻率
                        dopplerAnalyser.start(); //开启声音处理程序

                        Thread.sleep(2000);

                        float size = dopplerAnalyser.stop();//结束声音处理程序
                        System.out.println(size + "size of");
                        //把信息存在个人设置里面
                        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(FindUActivity.this).edit();
                        editor.putString(Constant.BASE_FREQUENCY_WIDTH, Float.toString(size));
                        editor.commit();
                        dopplerAnalyser.start();
                        while (!pm.getMsg().equals(Constant.STOP))
                            Thread.sleep(100);
                        pause();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
            }
        }.start();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void updateView() {
        while (listening) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            TextView text = (TextView) findViewById(R.id.text);
            String baseWidth = PreferenceManager.getDefaultSharedPreferences(this).getString(Constant.BASE_FREQUENCY_WIDTH, null);
            String width = PreferenceManager.getDefaultSharedPreferences(this).getString(Constant.MAX_FREQUENCY_WIDTH, null);
            if (null != baseWidth && null != width) {
                Float delta = Float.parseFloat(width) - Float.parseFloat(baseWidth);
                text.setText(delta.toString());
            }
        }
    }

    protected void pause() {
        float size = dopplerAnalyser.stop();
        System.out.println(size + "size of");
        SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(this).edit();
        editor.putString(Constant.MAX_FREQUENCY_WIDTH, Float.toString(size));
        editor.commit();
        String baseWidth = PreferenceManager.getDefaultSharedPreferences(this).getString(Constant.BASE_FREQUENCY_WIDTH, null);
        if (null != baseWidth) {
            Float delta = size - Float.parseFloat(baseWidth);
            pm.send("$" + delta + ":" + Build.MODEL);
        }
        pm.stopReceiver();
//        onPause();
        FindUActivity.this.updateView();
    }

    @Override
    protected void onPause() {
        listening = false;
        super.onPause();
    }
}
